class Compound extends Term {
    String name;
    Term[] args;
    
    int arity; // computed from args
    
    public Compound(String name, Term... args) {
        kind = Kind.COMPOUND;
        
        this.name = name;
        this.args = args;
        
        arity = args.length;
    }
    
    public Term deref() {
        return this;
    }
    
    public boolean contains(Term t) {
        for(int i = 0; i < arity; i++)
            if(args[i].deref() == t || args[i].contains(t))
                return true;
        return false;
    }
    
    public String toString() {
        String s = name;
        if(arity > 0) {
            s += "(";
            for(int i = 0; i < arity; i++) {
                s += args[i];
                if(i != arity-1) {
                    s += ", ";
                }
            }
            s += ")";
        }
        return s;
    }
    
    public char coerceToChar() throws ClassCastException {
        return (char)Integer.parseInt(name);
    }
    
    public String coerceToString() throws ClassCastException {
        if(arity == 0 && name.equals("[]")) {
            return "";
        }
        if(arity == 2 && name.equals(".")) {
            char head;
            String tail;
            head = args[0].coerceToChar();
            tail = args[1].coerceToString();
            return head + tail;
        }
        else {
            throw new ClassCastException(name +"/"+ arity + " cannot be part of a string");
        }
    }
}
